<template>
  <div class="app-container">
    <div class="filter-container">
      <el-form :inline="true" :model="listQuery" class="demo-form-inline">
        <el-form-item label="骑手名称">
          <el-input
            v-model.trim="listQuery.query"
            placeholder="请输入名称"
            style="width: 200px"
            clearable
            class="filter-item"
            @keyup.enter.native="handleFilter"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            v-waves
            class="filter-item"
            type="primary"
            icon="el-icon-search"
            @click="handleFilter"
          >搜索</el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="骑手名" width="80">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>

      <!-- <el-table-column width="180px" align="center" label="Date">
        <template slot-scope="{row}">
          <span>{{ row.timestamp | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column> -->

      <el-table-column width="120px" :align="'center'" label="骑手电话">
        <template slot-scope="{row}">
          <span>{{ row.serialNumber }}</span>
        </template>
      </el-table-column>
      <el-table-column :align="'center'" label="身份证号">
        <template slot-scope="{row}">
          <span>{{ row.idCard || '暂无信息' }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" :align="'center'" label="审核图片">
        <template slot-scope="{row}">
          <span><el-image
              :src="row.photoUrl"
              :preview-src-list="[row.photoUrl]">
            </el-image></span>
        </template>
      </el-table-column>
      <!-- <el-table-column width="100px" label="Importance">
        <template slot-scope="{row}">
          <svg-icon v-for="n in + row.importance" :key="n" icon-class="star" class="meta-item__icon" />
        </template>
      </el-table-column> -->

      <el-table-column class-name="status-col" label="状态" width="110">
        <template slot-scope="{row}">
          <el-tag :type="row.status | statusFilter">
            {{ row.status == '2' ? '待审核' : '使用中' }}
          </el-tag>
        </template>
      </el-table-column>

      <!-- <el-table-column min-width="300px" label="Title">
        <template slot-scope="{row}">
          <template v-if="row.edit">
            <el-input v-model="row.title" class="edit-input" size="small" />
            <el-button
              class="cancel-btn"
              size="small"
              icon="el-icon-refresh"
              type="warning"
              @click="cancelEdit(row)"
            >
              取消
            </el-button>
          </template>
          <span v-else>{{ row.title }}</span>
        </template>
      </el-table-column> -->

      <el-table-column :align="'center'" label="操作" width="120">
        <template slot-scope="{row}">
          <el-button
            v-if="row.status != 2"
            type="danger"
            size="small"
            @click="upDateItemStatus(row, 'stop')"
          >
            禁用
          </el-button>
          <el-button
            v-if="row.status == 2"
            type="success"
            size="small"
            @click="upDateItemStatus(row, 'pass')"
          >
            通过
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.pageNumber"
      :limit.sync="listQuery.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { getRiderListApi, updateAccountRiderApi } from '@/api/examine'
import Pagination from '@/components/Pagination' // secondary package based on el-pagination
export default {
  name: 'riderList',
  components: { Pagination },
  filters: {
    statusFilter(status) {
      const statusMap = {
        1: 'success', // 可用
        2: 'warning' // 待审核
      }
      return statusMap[status * 1]
    }
  },
  data() {
    return {
      list: null,
      listLoading: true,
      listQuery: {
        pageNumber: 1,
        pageSize: 10,
        query: ''
      },
      total: 0
    }
  },
  created() {
    // this.$message({
    //   message: '正在开发中',
    //   type: 'warning'
    // })
    this.getList()
  },
  methods: {
    handleFilter() {
      this.listQuery.pageNumber = 1
      this.getList()
    },
    getList() {
      this.listLoading = true
      getRiderListApi(this.listQuery)
        .then((response) => {
          this.list = response.accountList || []
          this.total = response.countPage || 0
          setTimeout(() => {
            this.listLoading = false
          }, 1000)
        })
        .catch(() => {
          setTimeout(() => {
            this.listLoading = false
          }, 1000)
        })
    },
    cancelEdit(row) {
      row.title = row.originalTitle
      row.edit = false
      this.$message({
        message: 'The title has been restored to the original value',
        type: 'warning'
      })
    },
    confirmEdit(row) {
      row.edit = false
      row.originalTitle = row.title
      this.$message({
        message: 'The title has been edited',
        type: 'success'
      })
    },
    upDateItemStatus(item, type) {
      this.$confirm(
        `确定要${type == 'pass' ? '通过' : '退回' }  ${item.name}  审核嘛?`,
        '提示',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(() => {
          const loading = this.$loading({
            lock: true,
            text: 'Loading',
            spinner: 'el-icon-loading',
            background: 'rgba(0, 0, 0, 0.7)'
          })
          const params = {
            accountId: item.id,
            status: (item.status == '0' || item.status == '1') ? '2' : '0'
          }
          updateAccountRiderApi(params)
            .then(() => {
              loading.close()
              this.getList()
              this.$message({
                type: 'success',
                message: '操作成功!'
              })
            })
            .catch(() => {
              loading.close()
            })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          })
        })

    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
